<template>
  <div class="box-main1">
    <div class="box-header2">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/index' }">控制台</el-breadcrumb-item>
        <el-breadcrumb-item>英语单词管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="box-main2">
      <div class="box-search2">

        <div class="box-input2" style="width: 180px">
          <el-input  @keydown.enter.native="clkBtnSearch" @clear="clkBtnSearch" clearable
                     size="mini" v-model="searchInfo.searchKey" placeholder="英文/中文"></el-input>
        </div>
        <div class="box-input2">
          <el-button size="mini" type="primary" icon="el-icon-search" @click="clkBtnSearch">搜索</el-button>
          <el-button size="mini" type="warning" icon="el-icon-plus" @click="clkBtnAdd">新增</el-button>
        </div>

      </div>

      <div class="box-content2">
        <div class="box-table2">
          <el-table class="tbl1" :data="englishWordPage.list" :stripe="true" size="small">

            <el-table-column type="index" :index="1" label="序号" width="55"></el-table-column>
            <el-table-column label="英文单词" width="180">
              <template slot-scope="scope">
                <span >{{scope.row.english_word}}</span>
              </template>
            </el-table-column>
            <el-table-column label="中文解释" width="190">
              <template slot-scope="scope">
                <span>{{scope.row.chinese}}</span>
              </template>
            </el-table-column>

            <el-table-column  label="操作" width="120">
              <template slot-scope="scope">
                 <span>
                   <el-button type="primary" size="mini" @click="clkBtnEdit(scope.row)">编辑</el-button>
                   <el-button type="warning" size="mini" @click="clkBtnDelete(scope.row )">删除</el-button>
                 </span>
              </template>
            </el-table-column>

          </el-table>
        </div>
        <div class="box-page2">
          <el-pagination layout="prev, pager, next" :total="englishWordPage.total"
                         :current-page="englishWordPage.pageNum" :page-size="englishWordPage.pageSize" @current-change="chgCurrPage">
          </el-pagination>
        </div>
      </div>
    </div>


    <el-dialog title="新增/编辑英文单词" class="dialog1"
               :show-close="false" :close-on-click-modal="false"
               :visible.sync="showEnglishWordDialog">
      <div>
        <div class="box-content2">
          <div class="box-table2">
            <el-form size="mini" ref="studentGroup" :model="englishWord" label-width="120px">
              <el-form-item label="英文单词">
                <el-input v-model="englishWord.english_word"></el-input>
              </el-form-item>
              <el-form-item label="中文解释">
                <el-input v-model="englishWord.chinese"></el-input>
              </el-form-item>
            </el-form>
          </div>

        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="clkBtnSave" >确 定</el-button>
        <el-button size="mini" @click="showEnglishWordDialog = false">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import request from '@/common/utils/request';

  export default {
    computed: {
      loginUserInfo() {
        return this.$store.state.loginUserInfo;
      },

    },
    data() {
      return {
        searchInfo: {searchKey: null},
        englishWordPage: {pageSize: 16, pageNum: 1 , list: []},

        pageLoading: false,

        showEnglishWordDialog: false,

        englishWord: {},

      };
    },
    mounted() {
      this._initData();
    },
    watch: {
      $route: {
        handler: function (to, from) {
          if (to.path == '/admin/english_word_list') {

          }

        }, immediate: true
      },
    },
    methods: {
      _initData() {
        this.getList();
      },

      clkBtnSearch() {
        this.getList();
      },
      getList() {
        let url1 = this.settings.apiUrl + '/english_word/page';
        let d1 = {pageNum: this.englishWordPage.pageNum, pageSize: this.englishWordPage.pageSize};
        d1.searchKey = this.searchInfo.searchKey;

        request.post(url1, d1).then((d1r) => {
          this.pageLoading = false;
          if (d1r.code === 0) {
            this.englishWordPage = d1r.data;
          } else {

            this.$alert('获取英语单词信息失败：' + d1r.msg, '提示', {
              type: 'warning'
            });

          }
        }).catch(err => {
          this.pageLoading = false;
        });
      },
      // 点击新增用户的按钮
      clkBtnAdd() {
        this.englishWord = {};
        this.showEnglishWordDialog = true;
      },
      clkBtnEdit(row) {
        this.englishWord = JSON.parse(JSON.stringify(row));
        this.showEnglishWordDialog = true;
      },

      clkBtnDelete(row) {

        this.$confirm('您确信要删除该记录吗', "提示").then(() => {

          let url1 = this.settings.apiUrl + '/english_word/delete';
          let d1 = {noid: row.noid};
          request.post(url1, d1).then((d1r) => {
            if (d1r.code === 0) {
              // 记录密码
              this.getList();
              this.$message('您删除成功了。');
            } else {
              this.$alert('删除英文单词失败：' + d1r.msg, '提示', {
                type: 'warning'
              });
            }
          }).catch(err => {
            this.loading = false;
            console.log(err);
          });

        })


      },
      chgCurrPage(val){
        this.englishWordPage.pageNum = val;
        this.getPage();
      },
      getPage(){
        this.getList();
      },

      clkBtnSave() {
        let url1 = this.settings.apiUrl + '/english_word/save';
        let d1 = JSON.parse(JSON.stringify(this.englishWord))  ;
        d1.create_user_id = this.loginUserInfo.noid;

        request.post(url1, d1).then((d1r) => {
          if (d1r.code === 0) {
            this.getList();
            this.showEnglishWordDialog = false;
          } else {
            this.$alert('保存英语单词失败：' + d1r.msg, '提示', {
              type: 'warning'
            });
          }
        }).catch(err => {
          this.loading = false;
          console.log(err);
        });


      }

    }

  };
</script>

<style lang="scss" scoped="scoped">

  .box-main2 {
    height: 100%;
    background-repeat: no-repeat;
    background-size: cover;
  }

  .box-header2{
    height: 30px;
    background-color: #eceeef;
    padding-left:20px;
    padding-top:8px;

  }

  .box-search2{
    display: flex;
  }
  .box-page2{
    display: flex;
    justify-content: flex-end;
  }

  .box-search2{
    padding-bottom: 10px;
    .box-input2{
      padding-right:10px;
    }
  }



</style>
